package com.codoon.unova.ui;

import java.util.Date;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.functionmanagement.util.StepOpenHelper;
import com.codoon.unova.bean.SportsBean;
import com.codoon.unova.bean.UserBean;
import com.codoon.unova.logic.SportsDataType;
import com.codoon.unova.util.ConfigManager;
import com.codoon.unova.util.DateUtil;
import com.codoon.unova.util.LogUtil;
import com.codoon.unova.util.NumberUtil;
import com.codoon.unova.view.CircleView;
import com.codoon.unova.view.MyLinearLayout;
import com.codoon.unova.view.MyLinearLayout.Direction;
import com.codoon.unova.view.MyLinearLayout.LayoutTouchLintener;
import com.uwatch.helowin.R;

/**
 * 
 * @author scott
 * 
 *         Function:数据显示部分的基类（sportsMainActivity，水平方向）
 */
public class HorizontalBaseActivity extends BaseActivity implements LayoutTouchLintener {
	private SQLiteDatabase mDb;
	private StepOpenHelper mOpenHelper;
	/** 记录滑动的界面 距离当前的日期多少天 */
	protected int currrentIndex;

	/** 今天的日期，【不管界面怎么切换，这个变量一直是当前日期[new Date()]】 */
	protected String str_date;
	/** 滑动到 当前的日期[这个时间是当前的时间，向前推currentIndex天所得到的时间] */
	protected String currentSelectedDate;
	// 其它界面传递过来的数据
	protected Bundle bundle;

	/* 界面通用的控件 */
	protected TextView tv_date;
	protected TextView tv_data;
	protected TextView tv_unit;
	/** 进度圆弧 */
	protected CircleView circleview;
	/* 可点击部分，点击该控件，进行数据类型的切换 */
	protected MyLinearLayout layout_sportInfo;

	/* 指定某一天的运动数据 */
	protected SportsBean bean;
	/** 数据类型：[步数，卡路里， 里程] */
	protected SportsDataType mDataType = SportsDataType.STEPS;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		currentSelectedDate = DateUtil.DateToString(new Date());
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		int index = currrentIndex;
		currentSelectedDate = DateUtil.addDate(str_date, -index);
		if ((currentSelectedDate != null) && !(currentSelectedDate.isEmpty())) {
			mOpenHelper = StepOpenHelper.getInstance(this);
			mDb = mOpenHelper.getWritableDatabase();
			searchStepsInDatabase(currentSelectedDate);
		}
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (bean != null) {
			tv_date.setText(bean.getDate());
			tv_data.setText(bean.getSteps() + "");
		} else {
			tv_date.setText(currentSelectedDate);
			tv_data.setText("0");
		}
		tv_unit.setText(getString(R.string.step_unit));
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		layout_sportInfo = (MyLinearLayout) findViewById(R.id.layout_sportinfo);
		layout_sportInfo.setOnClickListener(this);
		layout_sportInfo.setLayoutTouchLintener(this);

		tv_date = (TextView) findViewById(R.id.tv_date);
		tv_data = (TextView) findViewById(R.id.tv_data);
		tv_unit = (TextView) findViewById(R.id.tv_unit);
		tv_unit.setText(getString(R.string.step_unit));
		circleview = (CircleView) findViewById(R.id.circleview);

		if (bean == null) {
			circleview.setmSteps(0);
		} else {
			circleview.setmSteps(bean.getSteps());
		}
		circleview.setmRadius(68);
		int target = ConfigManager.getTarget(getApplicationContext());
		circleview.setmTarget(target == 0 ? 10000 : target);
		circleview.setmOuterColor(Color.parseColor("#9ff385"));
		circleview.setmOuterStrokeWidth(2);
		circleview.setmOverTargetColor(Color.parseColor("#9ff385"));
		circleview.setmInnerColor(Color.parseColor("#066c15"));
		circleview.setmInnerStrokeWidth(2);
	}

	@Override
	protected void showLeft() {
		// TODO Auto-generated method stub
		super.showLeft();
		bundle = new Bundle();
		bundle.putInt("currentIndex", currrentIndex + 1);
		bundle.putString("date", str_date);
		changeView(SportsTwoActivity.class, bundle, true);
		overridePendingTransition(R.anim.right_in, R.anim.right_out);
	}

	@Override
	protected void showRight() {
		// TODO Auto-generated method stub
		super.showRight();
		if (currrentIndex > 1) {
			bundle = new Bundle();
			bundle.putInt("currentIndex", currrentIndex - 1);
			bundle.putString("date", str_date);
			changeView(SportsOneActivity.class, bundle, true);
			overridePendingTransition(R.anim.left_in, R.anim.left_out);
		} else {
			changeView(SportsMainActivity.class, null, true);
			overridePendingTransition(R.anim.left_in, R.anim.left_out);
		}
	}

	@Override
	protected void showNext() {
		// TODO Auto-generated method stub
		super.showNext();
		ConfigManager.setStringValue(getApplicationContext(), ConfigManager.SELECTED_DATE, currentSelectedDate);
		ConfigManager.setIntValue(getApplicationContext(), ConfigManager.CURRENT_INDEX, currrentIndex);
		changeView(SettingTargetActivity.class, null, true);
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.layout_sportinfo:
			chagneData();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mDataType = SportsDataType.STEPS;
	}

	protected void chagneData() {
		switch (mDataType) {
		case CALORIES:
			if (bean != null) {
				tv_data.setText(NumberUtil.decimalFloat(bean.getMeters()) + "");
			} else {
				tv_data.setText("0");
			}
			tv_unit.setText(getString(R.string.km_unit));
			mDataType = SportsDataType.METERS;
			break;
		case METERS:
			if (bean != null) {
				tv_data.setText((bean.getSteps()) + "");
			} else {
				tv_data.setText("0");
			}
			tv_unit.setText(getString(R.string.step_unit));
			mDataType = SportsDataType.STEPS;
			break;
		case STEPS:
			if (bean != null) {
				tv_data.setText(NumberUtil.decimalFloat(bean.getCalories()) + "");
			} else {
				tv_data.setText("0");
			}
			tv_unit.setText(getString(R.string.kcal_unit));
			mDataType = SportsDataType.CALORIES;
		default:
			break;
		}
	}

	@Override
	public void changeView(Direction status) {
		// TODO Auto-generated method stub
		if (status == Direction.LEFT) {
			showLeft();
		} else if (status == Direction.REIGHT) {
			showRight();
		} else if (status == Direction.PREVIOUS) {
			showNext();
		}
	}
	
	public void searchStepsInDatabase(String date) {
		if (mDb == null) {
			return;
		}
		try {
			Cursor c = mDb.query(StepOpenHelper.DATABASE_TABLE,
					new String[] {StepOpenHelper.KEY_DATE, StepOpenHelper.KEY_SETPS }, "date=?",
					new String[] { date }, null, null, null);
			LogUtil.d("HorizontalBaseActivity", " date = " + date+ " c = " + (c != null ? c.getCount() : "NULL"));
			if (c != null && c.getCount() > 0) {
				c.moveToFirst();
				UserBean userbean = ConfigManager.getUserInfo(HorizontalBaseActivity.this);
				int _currSteps = c.getInt(c.getColumnIndex(StepOpenHelper.KEY_SETPS));
				float _currMetersKm = userbean.getStridelength() * _currSteps / 1000;
				float _currCalories = userbean.getWeight() * _currMetersKm * 1.036f; //// calorie(Kcal)=体重(kg)*路程(km)*1.036;  单位千卡			
				if ((_currSteps >= 0) && (_currMetersKm >= 0)  && (_currCalories >= 0)) {
					bean = new SportsBean(_currSteps, _currCalories, _currMetersKm, date);
				}
				LogUtil.d("HorizontalBaseActivity", "==_currSteps:" +_currSteps+"==_currCalories:" +_currCalories+"==_currMetersKm:" +_currMetersKm+"==date:" +date);
				c.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	}
}
